package com.akcome.apollo.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.akcome.apollo.domain.VFoundBlocks;
import com.akcome.apollo.repository.VFoundBlocksRepository;
import com.akcome.apollo.service.VFoundBlocksService;


/**
 * Service接口实现
 *
 * @author wn
 * @version $v: 1.0.0, $time:2017-12-26 08:49:00 Exp $
 */
@Service
public class VFoundBlocksServiceImpl implements VFoundBlocksService {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private VFoundBlocksRepository vFoundBlocksRepository;
	
	@Override
	public VFoundBlocks save(VFoundBlocks vFoundBlocks){
		return vFoundBlocksRepository.save(vFoundBlocks);
	}
	
	@Override
	public VFoundBlocks findOne(Long id){
		return vFoundBlocksRepository.findOne(id);
	}
	
	@Override
	public Page<VFoundBlocks> findByPage(final String coinType,Pageable page){
		return vFoundBlocksRepository.findAll(new Specification<VFoundBlocks>() {
			
			@Override
			public Predicate toPredicate(Root<VFoundBlocks> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> params = new ArrayList<>();
				if(StringUtils.isNotBlank(coinType)){
					params.add(cb.equal(root.get("cointype"), coinType));
				}
				Predicate [] predicates = new Predicate[params.size()];
				query.where(params.toArray(predicates));
				query.orderBy(cb.desc(root.get("height")));
				return null;
			}
		},page);
	}
	
	@Override
	public List<Object> findGroupCoinType(){
		return vFoundBlocksRepository.findGroupCoinType();
	}
}
